/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import bo.UserBO;
import dbmapper.UserMapper;
import java.io.IOException;
import java.io.PrintWriter;

import javabean.UserBean;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Admin
 */
@WebServlet(name = "DangKyServlet", urlPatterns = {"/DangKyServlet"})
public class DangKyServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        
        UserMapper usermap = new UserMapper();

        UserBean user;
        user = new UserBean();
        UserBean user1 = new UserBean();
        UserBO userbo = new UserBO();
        try {

            /* TODO output your page here. You may use following sample code. */


            user.setUsername(req.getParameter("username"));

            user.setPassword(req.getParameter("password"));

            user.setSex(req.getParameter("sex"));

            user.setFullname(req.getParameter("fullname"));

            user.setAddress(req.getParameter("address"));

            user.setEmail(req.getParameter("email"));

            //out.print(user.getUsername());
            //out.print(req.getParameter("username"));
            //
            //out.print(user.getPassword());
            //out.print(req.getParameter("password"));
            //
            //out.print(user.getFullname());
            //out.print(req.getParameter("fullname"));
            //
            //out.print(user.getAddress());
            //out.print(req.getParameter("address"));
            //
            //out.print(user.getEmail());
            //out.print(req.getParameter("email"));
            //
            //out.print(user.getSex());
            //out.print(req.getParameter("sex"));

            user1 = userbo.isExist(req.getParameter("username"), req.getParameter("password"));

            HttpSession session = req.getSession();
            session.setAttribute("userbean", user); //dua ten username vao session

            if (user1 != null) {
                out.print("Tên đăng nhập đã bị trùng");

            } else {
                try {
                    usermap.createNewUser(user);
                    //out.print("Tên đăng nhập đã bị trùng");
                    userbo.InsertUser(user);
                    out.print("<link href=\"../../CSS/Site1.css\" type=\"text/css\" rel=\"stylesheet\"/>");
                   resp.sendRedirect("jsp/Account/Register_Succeeded.jsp");
                } catch (Exception ex) {
                    out.print(ex.getStackTrace());
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            out.close();
        }


        // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
        /**
         * Handles the HTTP
         * <code>GET</code> method.
         *
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
        /**
         * Handles the HTTP
         * <code>POST</code> method.
         *
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
    }
}
